package com.itbaizhan.shopping_cart_service.service;

import com.itbaizhan.shopping_common.pojo.CartGoods;
import com.itbaizhan.shopping_common.service.CartService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 购物车服务实现类
 */
@DubboService
@Service
public class CartServiceImpl implements CartService {
	
	private static final String CART_LIST = "cartList";//redis中购物车列表的key
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * 添加购物车
	 *
	 * @param userId
	 * @param cartGoods
	 */
	public void addCart(Long userId, CartGoods cartGoods) {
		
		// 1.根据用户id获取用户购物车列表
		List<CartGoods> cartList = findCartList(userId);
		// 2.查询购物车是否有该商品，如果有商品，添加商品数量
		for (CartGoods cartGoods1 : cartList) {
			if (cartGoods.getGoodId().equals(cartGoods1.getGoodId())){
				int newNum = cartGoods1.getNum() + cartGoods.getNum();
				cartGoods1.setNum(newNum);
				redisTemplate.boundHashOps(CART_LIST).put(userId,cartList);
				System.out.println("添加购物车成功");
				return;
			}
		}
		// 3.如果购物车没有该商品，将商品添加到购物车列表
		cartList.add(cartGoods);
		redisTemplate.boundHashOps(CART_LIST).put(userId,cartList);
		System.out.println("将商品添加到购物车列表成功");
	}
	
	/**
	 * 修改购物车商品数量
	 *
	 * @param userId
	 * @param goodId
	 * @param num
	 */
	public void handleCart(Long userId, Long goodId, Integer num) {
		// 获取用户购物车列表
		List<CartGoods> cartList = findCartList(userId);
		
		for (CartGoods cartGoods : cartList) {
			if (cartGoods.getGoodId().equals(goodId)) {
				cartGoods.setNum(num);
				break;
			}
			
		}
		// 将新的购物车列表保存到redis中
		redisTemplate.boundHashOps(CART_LIST).put(userId, cartList);
	}
	
	
	/**
	 * 更新redis中的商品数据，在管理员更新商品后执行
	 *
	 * @param cartGoods
	 */
	public void refreshCartGoods(CartGoods cartGoods) {
		//TODO 方法效率低
		
		// 获取所有用户的购物车
		BoundHashOperations cartList = redisTemplate.boundHashOps(CART_LIST);
		Map<Long,List<CartGoods>> allCartGoods = cartList.entries();
		Collection<List<CartGoods>> values = allCartGoods.values();
		
		// 遍历所有用户的购物车
		for (List<CartGoods> goodsList : values) {
			// 遍历一个用户购物车的所有商品
			for (CartGoods goods : goodsList) {
				// 如果该商品是被更新的商品，修改商品数据
				if (cartGoods.getGoodId().equals(goods.getGoodId())){
					goods.setGoodsName(cartGoods.getGoodsName());
					goods.setHeaderPic(cartGoods.getHeaderPic());
					goods.setPrice(cartGoods.getPrice());
				}
			}
		}
		
		// 将改变后所有用户购物车重新放入redis
		redisTemplate.delete(CART_LIST);
		redisTemplate.boundHashOps(CART_LIST).putAll(allCartGoods);
	}
	
	/**
	 * 获取用户购物车
	 *
	 * @param userId
	 * @return
	 */
	public List<CartGoods> findCartList(Long userId) {
		
		Object cartList = redisTemplate.boundHashOps(CART_LIST).get(userId);
		if (cartList == null) {
			return new ArrayList<CartGoods>();
		} else {
			return (List<CartGoods>) cartList;
		}
		
	}
	
	/**
	 * 删除购物车商品
	 *
	 * @param userId
	 * @param goodId
	 */
	public void deleteCartOption(Long userId, Long goodId) {
		// 获取用户购物车列表
		List<CartGoods> cartList = findCartList(userId);
		// 将商品移出列表
		for (CartGoods cartGoods : cartList) {
			if (goodId.equals(cartGoods.getGoodId())) {
				cartList.remove(cartGoods);
				break;
			}
		}
		// 将新的购物车列表保存到redis中
		redisTemplate.boundHashOps(CART_LIST).put(userId, cartList);
		System.out.println("删除购物车商品成功");
	}
	
	/**
	 * 删除redis中的商品数据，在管理员下架商品后执行
	 *
	 * @param goodId
	 */
	public void deleteCartGoods(Long goodId) {
		//TODO 方法效率低
		
		// 获取所有用户购物车
		BoundHashOperations cartList = redisTemplate.boundHashOps(CART_LIST);
		Map<Long,List<CartGoods>> allCartGoods = cartList.entries();
		Collection<List<CartGoods>> values = allCartGoods.values();
		
		// 遍历所有用户的购物车
		for (List<CartGoods> goodsList : values) {
			// 遍历一个用户购物车的所有商品
			for (CartGoods goods : goodsList) {
				// 如果该商品是被下架的商品，在购物车删除该商品
				if (goodId.equals(goods.getGoodId())){
					goodsList.remove(goods);
					break;
				}
			}
		}
		
		// 将改变后所有用户购物车重新放入redis
		redisTemplate.delete(CART_LIST);
		redisTemplate.boundHashOps(CART_LIST).putAll(allCartGoods);
	}
}
